﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestorDeFlotasDesktop.Classes;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AbmRol
{
    public partial class frmRol : Form
    {
        private GestorDeFlotasDesktop.AbmRol.frmNuevoRol frameNuevoRol;

        public frmRol()
        {
            InitializeComponent();
        }

        private void cargarRoles()
        {
            Roles oRoles = new Roles();
            SqlInterface.Conectar();
            dgvRoles.DataSource = oRoles.getRoles();
            SqlInterface.Desconectar();
            
        }
        private void frmRol_Load(object sender, EventArgs e)
        {
            cargarRoles();

        }

        private void btnCrear_Click(object sender, EventArgs e)
        {
            frameNuevoRol = new GestorDeFlotasDesktop.AbmRol.frmNuevoRol();
            frameNuevoRol.MdiParent = this.MdiParent;
            frameNuevoRol.cargarCrearRol();
            frameNuevoRol.Show();
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            //El string del cell que manda es MALISIMO
            Rol oRol = (Rol)dgvRoles.CurrentRow.DataBoundItem;
            frameNuevoRol = new GestorDeFlotasDesktop.AbmRol.frmNuevoRol();
            frameNuevoRol.oRol = oRol;
            frameNuevoRol.MdiParent = this.MdiParent;
            frameNuevoRol.cargarModificarRol();
            frameNuevoRol.Show();
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (dgvRoles.SelectedRows.Count == 1)
            {
                SqlInterface.Conectar();
                List<SqlParameter> ParameterList = new List<SqlParameter>();

                ParameterList.Add(new SqlParameter("@rol", ((Rol)dgvRoles.CurrentRow.DataBoundItem).intCodigo));
                if (SqlInterface.ExecuteNonQuery("SQUIRTLE.spBajarRol", CommandType.StoredProcedure, ParameterList) != 1
                    & SqlInterface.ExecuteNonQuery("SQUIRTLE.spBajarRolesUsuarios",ParameterList) != 1)
                {
                    MessageBox.Show("Error al eliminar rol");
                }
                SqlInterface.Desconectar();
                MessageBox.Show("Se ha eliminado el rol con exito.");
                cargarRoles();
            }
            else
            {
                MessageBox.Show("Debe seleccionar un rol de la lista.");
            }
        }

        private void dgvRoles_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Rol oRol = (Rol)dgvRoles.CurrentRow.DataBoundItem;
            frameNuevoRol = new GestorDeFlotasDesktop.AbmRol.frmNuevoRol();
            frameNuevoRol.oRol = oRol;
            frameNuevoRol.cargarModificarRol();
            frameNuevoRol.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cargarRoles();
        }
    }
}
